<template>
	<view>
		<tui-sticky :scrollTop="scrollTop" stickyHeight="51px" container>
			<view class="padding bg-white" slot="header" ref="sticky">
				<u-search
					@search="list"
					placeholder="公司名称查询"
					v-model="pageRequest.condition.orgName"
					action-text="添加"
					@custom="navTo(`edit?parentId=${pageRequest.condition.parentId}`)"
				></u-search>
			</view>
			<template slot="content">
				<view class="margin-top">
					<view class="solid-bottom" v-for="(item, index) in pageData.content" :key="item.id">
						<tui-swipe-action :showMask="false" :actions="actions" @click="edit($event, item)">
							<template v-slot:content>
								<u-cell-item
									:title-style="{ width: '100%' }"
									icon="home"
									:title="item.aliasName"
									:arrow="true"
									arrow-direction="right"
									@click="navTo('list?parentId=' + item.id)"
								>
									<view slot="label">
										<view>全称：{{ item.orgName }}</view>
										<view>编码：{{ item.orgCode }}</view>
										<view>类型：{{ item.orgTypeName }}</view>
										<view>序号：{{ item.sequence || 0 }}</view>
									</view>
								</u-cell-item>
							</template>
						</tui-swipe-action>
					</view>
					<u-loadmore :status="pageData.page < pageData.pages ? 'loading' : 'nomore'" bg-color="" class="padding" />
				</view>
			</template>
		</tui-sticky>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		this.pageRequest.condition.parentId = option.parentId || '';
		if (this.pageRequest.condition.parentId) {
			this.list(true);
		}
	},
	onPullDownRefresh:function(){
		this.list(true);
	},
	onReachBottom: function() {
		this.list();
	},
	data() {
		return {
			scrollTop: 0, // 滚动高度
			actions: [
				{
					name: '修改',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#ffaa00'
				},
				{
					name: '删除',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#ff0000'
				},
				{
					name: '角色',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#5555ff'
				}
			],
			pageRequest: {
				page: 1,
				rows: 15,
				sort: 'sequence:asc',
				condition: {
					parentId: '',
					orgCode: '',
					orgName: ''
				}
			},
			pageData: {
				page: 1,
				pages: 1,
				content: []
			}
		};
	},
	//页面滚动执行方式
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	methods: {
		navTo: function(url) {
			uni.navigateTo({
				url: url
			});
		},
		list: function(first) {
			if (first) {
				this.pageRequest.page = 1;
			} else {
				if (this.pageData.page < this.pageData.pages) {
					this.pageRequest.page = this.pageRequest.page + 1;
				} else {
					return;
				}
			}
			if (this.pageRequest.condition.parentId) {
				this.$request.post(
					`/admin/sys/org/list`,
					this.pageRequest,
					res => {
						let pageData = res.data || {};
						if (this.pageRequest.page > 1) {
							pageData.content = (this.pageData.content || []).concat(pageData.content);
						}
						this.pageData = pageData;
					},
					false,
					true,
					true
				);
			}
		},
		edit: function(e, item) {
			if (e.index == 0) {
				this.navTo(`edit?info=${JSON.stringify(item)}`);
			} else if (e.index == 1) {
				this.$utils.showModal(
					'确认删除？',
					() => {
						this.$request.post(
							'/admin/sys/org/delete',
							item,
							res => {
								this.list();
							},
							true,
							true,
							true
						);
					},
					true
				);
			} else if (e.index == 2) {
				uni.navigateTo({
					url: `role-list?info=${encodeURIComponent(JSON.stringify(item))}`
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped></style>
